package com.fy.springmvc.controller;

import com.alibaba.fastjson.JSON;
import com.fy.springmvc.enetity.Vo;
import com.fy.springmvc.enetity.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/user/")
public class UserController {
    @RequestMapping("q1.do")
    public void s1(HttpServletResponse response) throws IOException {
    response.getWriter().write("hello word");
    }


    @RequestMapping("q2.do")
    @ResponseBody  //告知springmvc框架 该方法不进行视图跳转 直接进行数据响应
    public String  s2(){
      return "hello word";
    }

    @RequestMapping("q3.do")
    @ResponseBody  //告知springmvc框架 该方法不进行视图跳转 直接进行数据响应
    public String  s3(){
        return "{\"name\":\'zhangsan\', \"age\":23}";
    }

    @RequestMapping("q4.do")
    @ResponseBody  //告知springmvc框架 该方法不进行视图跳转 直接进行数据响应
    public String  s4(){
        User user = new User();
        user.setName("lisan");
        user.setAge(12);
        //使用JSON转换工具 将对象转换成JSON格式字符串
        return JSON.toJSONString(user);
    }

    @RequestMapping("q5.do")
    @ResponseBody  //告知springmvc框架 该方法不进行视图跳转 直接进行数据响应
    public User  s5(){
        User user = new User();
        user.setName("lisan");
        user.setAge(12);
        //使用JSON转换工具 将对象转换成JSON格式字符串
        return user;
    }

    @RequestMapping("q6.do")
    @ResponseBody
    public void q6(String name, String age) throws IOException {

        System.out.println(name);
        System.out.println(age);
    }

    @RequestMapping("q7.do")
    @ResponseBody
    public void q7(User user) throws IOException {
        System.out.println(user);
    }

    @RequestMapping("q8.do")
    @ResponseBody
    public void q8(String[] str) throws IOException {
        System.out.println(Arrays.toString(str));
    }

//    @RequestMapping("qq.do")
//    @ResponseBody
//    public void q9(@RequestBody List<User> userList) throws IOException {
//        System.out.println(userList);
//    }

    @RequestMapping("q10.do")
    public void q10(User user) throws IOException {
        System.out.println(user);
    }
}
